home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Source / Property Editors / ixedit.pas < prev    next >
Pascal/Delphi Source File  |  1999-08-11  |  5KB  |  190 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {       IndexFileName Property Editor Dialog            }
  6. {                                                       }
  7. {       Copyright (c) 1997,99 Inprise Corporation       }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit IxEdit;
  12.  
  13. interface
  14.  
  15. uses
  16.   SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs,
  17.   StdCtrls, DBTables, LibHelp;
  18.  
  19. type
  20.   TIndexFiles = class(TForm)
  21.     GroupBox1: TGroupBox;
  22.     ListBox1: TListBox;
  23.     Add: TButton;
  24.     Delete: TButton;
  25.     Ok: TButton;
  26.     Cancel: TButton;
  27.     Help: TButton;
  28.     Clear: TButton;
  29.     OpenDialog: TOpenDialog;
  30.     procedure ListBox1Click(Sender: TObject);
  31.     procedure AddClick(Sender: TObject);
  32.     procedure DeleteClick(Sender: TObject);
  33.     procedure ClearClick(Sender: TObject);
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure HelpClick(Sender: TObject);
  36.   private
  37.     FTable: TTable;
  38.     FNoItems: Boolean;
  39.     FEmpty: string;
  40.     procedure AddEmpty;
  41.     function IsDBaseTable: Boolean;
  42.     procedure RemoveEmpty;
  43.     procedure SetButtons;
  44.   public
  45.     property Table: TTable read FTable;
  46.     property NoItems: Boolean read FNoItems;
  47.   end;
  48.  
  49. function EditIndexFiles(ATable: TTable; List: TStrings): Boolean;
  50.  
  51. implementation
  52.  
  53. {$R *.DFM}
  54.  
  55. uses Bde, BdeConst, TypInfo, DBConsts;
  56.  
  57. function GetPath(const Path: string): string;
  58. var
  59.   Desc: DBDesc;
  60.   SAliasName: array [0..DBIMAXNAMELEN - 1] of char;
  61. begin
  62.   if ExtractFileDir(Path) = '' then
  63.   begin
  64.     StrPLCopy(SAliasName, Path, SizeOf(SAliasName) - 1);
  65.     AnsiToOem(SAliasName, SAliasName);
  66.     if DbiGetDatabaseDesc(SAliasName, @Desc) = 0 then
  67.       if StrPas(Desc.szDbType) = 'STANDARD' then
  68.         Result := StrPas(Desc.szPhyName);
  69.   end
  70.   else Result := Path;
  71. end;
  72.  
  73. function TIndexFiles.IsDBaseTable: Boolean;
  74. begin
  75.   Result := (Table.TableType = ttDBase) or
  76.     (CompareText(ExtractFileExt(Table.TableName), '.DBF') = 0);
  77. end;
  78.  
  79. function EditIndexFiles(ATable: TTable; List: TStrings): Boolean;
  80. begin
  81.   Session.Open;
  82.   with TIndexFiles.Create(Application) do
  83.     try
  84.       FTable := ATable;
  85.       Caption := Format(SIndexFilesEditor,
  86.         [Table.Owner.Name, DotSep, Table.Name]);
  87.       if IsDBaseTable then ListBox1.Items.Assign(List)
  88.       else Add.Enabled := False;
  89.       OpenDialog.InitialDir := GetPath(Table.DatabaseName);
  90.       if ListBox1.Items.Count = 0 then AddEmpty;
  91.       SetButtons;
  92.       Result := ShowModal = mrOk;
  93.       if Result then
  94.       begin
  95.         RemoveEmpty;
  96.         List.Assign(ListBox1.Items);
  97.       end;
  98.     finally
  99.       Free;
  100.     end;
  101. end;
  102.  
  103. procedure TIndexFiles.SetButtons;
  104. var
  105.   I: Integer;
  106. begin
  107.   if not IsDBaseTable then Exit;
  108.   Clear.Enabled := not NoItems;
  109.   if NoItems then Delete.Enabled := False
  110.   else with ListBox1 do
  111.   begin
  112.     for I := Items.Count - 1 downto 0 do
  113.       if Selected[I] then Break;
  114.     Delete.Enabled := I <> -1;
  115.   end;
  116. end;
  117.  
  118. procedure TIndexFiles.ListBox1Click(Sender: TObject);
  119. begin
  120.   SetButtons;
  121. end;
  122.  
  123. procedure TIndexFiles.AddClick(Sender: TObject);
  124. begin
  125.   with OpenDialog, ListBox1.Items do
  126.     if Execute then
  127.     begin
  128.       if IndexOf(FileName) = -1 then
  129.       begin
  130.         RemoveEmpty;
  131.         Add(ExtractFileName(FileName));
  132.       end;
  133.       SetButtons;
  134.     end;
  135. end;
  136.  
  137. procedure TIndexFiles.AddEmpty;
  138. begin
  139.   if not FNoItems then
  140.   begin
  141.     ListBox1.Items.Add(FEmpty);
  142.     FNoItems := True;
  143.   end;
  144. end;
  145.  
  146. procedure TIndexFiles.RemoveEmpty;
  147. begin
  148.   if FNoItems then
  149.   begin
  150.     ListBox1.Items.Delete(0);
  151.     FNoItems := False;
  152.   end;
  153. end;
  154.  
  155. procedure TIndexFiles.DeleteClick(Sender: TObject);
  156. var
  157.   I: Integer;
  158. begin
  159.   with ListBox1 do
  160.   begin
  161.     for I := Items.Count - 1 downto 0 do
  162.       if Selected[I] then Items.Delete(I);
  163.     if Items.Count = 0 then AddEmpty;
  164.   end;
  165.   SetButtons;
  166. end;
  167.  
  168. procedure TIndexFiles.ClearClick(Sender: TObject);
  169. begin
  170.   with ListBox1 do
  171.   begin
  172.     Clear;
  173.     AddEmpty;
  174.   end;
  175.   SetButtons;
  176. end;
  177.  
  178. procedure TIndexFiles.FormCreate(Sender: TObject);
  179. begin
  180.   FEmpty := SNoIndexFiles;
  181.   HelpContext := hcDTableIndexEditor;
  182. end;
  183.  
  184. procedure TIndexFiles.HelpClick(Sender: TObject);
  185. begin
  186.   Application.HelpContext(HelpContext);
  187. end;
  188.  
  189. end.
  190.